import store from '@/store/index.js';
const siteroot = (params,type=0) => {
	if(type == 3){
		let siteroot = uni.getStorageSync('siteroot');
		let url = siteroot
		return url;
	}else{
		let url = getsiteurl('index.php', params)
		return url;
	}
}

const getsiteurl = (path, params) => {
	 //let url = 'https://booking.melab514.cn/index.php/'
	let url = 'http://8.134.146.34/index.php/'
	let siteroot = uni.getStorageSync('siteroot');
	if (siteroot) {
		url = siteroot
	}
	if(path){
		if (url.substring(url.length - 1) == '/') {
			url = url + path;
		} else {
			url = url + path;
		}
	}
	
	if (window.location.hostname == 'localhost') {
		// url = 'http://localhost:' + window.location.port + '/' + path
	}
	if (params) url = url + '?' + params;
	console.log('params url=',url)
	return url;
}

const request = (path, param, type, method = 'GET', istoken = true) => {
	// uni.showLoading({
	// 	mask: true,
	// 	title: '加载中...'
	// });
	
	method = method.toUpperCase() 
	
	let url = "";
	if(method=='POST'){
		url = getsiteurl(path,'');
	}else{
		url = getsiteurl(path,param);
	}
	
	let header = {};
	let token = uni.getStorageSync("token")
	//url += `${path}`;
	console.log('--请求---', url);

	let conf = {
		//token: token,
	}

	//请求方式
	// if (method) {
	// 	//方法转换大小
	// 	if (method == "POST") {
	// 		header = {
	// 			'Token': token,
	// 			'X-Requested-With': 'XMLHttpRequest',
	// 			"Accept": "application/json",
	// 			"content-type": "application/x-www-form-urlencoded"
	// 		}
	// 	} else if (method == "GET") {

	// 		header = {
	// 			'Token': token,
	// 			'X-Requested-With': 'XMLHttpRequest',
	// 			"Accept": "application/json",
	// 			"Content-Type": "application/json; charset=UTF-8"
	// 		}

	// 	}
	// } 
console.log('params header=',header) 
	function getSearchString(key, Url) {
		var str = Url;
		var paras = str.split("=");
		return paras[1]
	}

	if (token || !istoken) {
		return new Promise(function(resolve, reject) {
			uni.request({
				url: url,
				method: method,
				timeout: 30000,
				data: {
					...conf,
					...param
				},
				dataType: 'json',
				header: header,
				success: (res) => {
					console.log('success res==', res)
					resolve(res)
				},
				// 这里的接口请求，如果出现问题就输出接口请求失败
				fail: (err) => {
				  console.log('fail err==', err)
					uni.showModal({
						title: '网络异常',
						content: JSON.stringify(err),
						showCancel: false,
						success: function() {
							//getApp().autologin()
						}
					})
					reject(err)
				},
				complete: () => {
					uni.hideLoading();
					return;
				}
			})
		})
	} else {
		//getApp().autologin()
	}


}


export default {
	request,
	siteroot,
	getsiteurl,
}